package com.itcncp.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

	public static final String datePattern = "yyyy-MM-dd HH:mm:ss";
	public static final String dateCnPattern = "yyyy年MM月dd日HH时mm分";

	/**
	 * 返回格式：yyyy-MM-dd
	 * @return
	 */
	public static String getToday(){
		String time = "";
		time = getToday("yyyy-MM-dd");
		return time;
	}
	/**
	 *
	 * @param format 根据指定的格式时间类型返回当前时间
	 * @return
	 */
	public static String getToday(String format){
		return getDateStr(Calendar.getInstance().getTime(),format);
	}

	/**
	 * 日期转字符
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getDateStr(Date date,String format){
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}

	/**
	 * @param millis
	 * @return
	 */
	public static Date parseMills(long millis){
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millis);
		return calendar.getTime();
	}

	/**
	 * 考试年份编码
	 * @return
	 */
	public static String getYearCode(){
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		String yearStr = new Integer(year).toString();
		//当前年份后两位
		return yearStr.substring(2,4);
	}

	/**
	 * String日期转Date
	 *
	 * @param dateStr
	 * @param format
	 * @return 转换失败返回null
	 */
	public static Date parseDate(String dateStr, String format) {
		SimpleDateFormat fmt = new SimpleDateFormat(format);
		try {
			return fmt.parse(dateStr);
		} catch (ParseException e) {
			return null;
		}
	}

	public static long diffTime(Date date,Date date2){
		long diffTime = date.getTime() - date2.getTime();
		return diffTime/1000;
	}
	/**
	 * 时间加减处理
	 * @param date
	 * @param field
	 * @param amount
	 * @return
	 */
	public static Date add(Date date,int field,int amount){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(field, amount);
		return calendar.getTime();
	}
	/**
	 * 时间加减处理
	 * @param dateStr
	 * @param format
	 * @param field
	 * @param amount
	 * @return
	 */
	public static String add(String dateStr,String format,int field,int amount){
		Date date = parseDate(dateStr, format);
		date = add(date, field, amount);
		return getDateStr(date, format);
	}


	/**
	 * 格式化时间
	 * @param date
	 * @param format
	 * @return
	 */
	public static String formatDate(Date date, String format) {
		SimpleDateFormat tofmt = new SimpleDateFormat(format);
		return tofmt.format(date);
	}

	/**
	 * 日期格式化
	 * @param year
	 * @param month
	 * @param day
	 * @param format
	 * @return
	 */
	public static String getDateStr(int year, int month, int day, String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		return formatDate(calendar.getTime(), format);
	}

	/**
	 * 日期格式化
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public static String getDateStr(int year, int month, int day) {
		return getDateStr(year, month, day, "yyyyMMdd");
	}

	/**
	 * 时间格式化
	 * @param format
	 * @return
	 */
	public static String getTimeStr(int hour, int minute, int second, String format) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		return formatDate(calendar.getTime(), format);
	}

	/**
	 * 时间格式化
	 * @return
	 */
	public static String getTimeStr(int hour, int minute, int second) {
		return getTimeStr(hour, minute, second, "HHmmss");
	}

	/**
	 * 格式化
	 * @param date
	 * @param format
	 * @param toFormat
	 * @return
	 */
	public static String formatDateStr(String date, String format, String toFormat){
		SimpleDateFormat fmt = new SimpleDateFormat(format);
		SimpleDateFormat tofmt = new SimpleDateFormat(toFormat);
		Date d;
		try {
			d = fmt.parse(date);
			return tofmt.format(d);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}


	public static String dateFormat(Date date) {

		return dateFormat(date,datePattern);
	
	}
	
	public static String dateFormat(Date date, String datePattern) {
		if (datePattern == null)
			datePattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.UK);
		return df.format(date);
	}

	public static String dateFormatNow(String datePattern) {
		if (datePattern == null)
			datePattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.UK);
		return df.format(new Date());
	}

	/**
	 * 当前日期转换成时间戳
	 * 
	 * @param datePattern
	 * @return
	 * @throws ParseException
	 */
	public static Long timeFormatNow(String datePattern) throws ParseException {
		if (datePattern == null)
			datePattern = "yyyy-MM-dd HH:mm:ss";

		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.UK);

		String times = df.format(new Date());

		Date date = df.parse(times);

		return date.getTime()/1000;
	}

	/**
	 * 当前日期转换成时间戳
	 * 
	 * @param datePattern
	 * @return
	 * @throws ParseException
	 */
	public static Long timeFormatDate(Date date, String datePattern)  {
		if (datePattern == null)
			datePattern = "yyyy-MM-dd HH:mm:ss";

		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.UK);

		String times = df.format(date);

		Date itemDate = new Date();
		try {
			itemDate = df.parse(times);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return itemDate.getTime()/1000;
	}

	/**
	 * 当前日期转换成时间戳
	 * @return
	 * @throws ParseException
	 */
	public static Long timeFormatDate(Date date) {
		return timeFormatDate(date, null);
	}

	/**
	 * 时间戳转换成日期
	 * 
	 * @param datePattern
	 * @return
	 * @throws ParseException
	 */
	public static Date dateFormatTimes(String times, String datePattern) {
		if (datePattern == null)
			datePattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.CHINA);

		Long time = Long.parseLong(times) * 1000L;

		String d = df.format(time);

		Date date;
		try {
			date = df.parse(d);
		} catch (ParseException e) {
			return new Date();
		}

		return date;
	}

	/**
	 * 时间戳转换成日期
	 * @return
	 * @throws ParseException
	 */
	public static Date dateFormatTimes(String times)  {
		return dateFormatTimes(times, null);
	}

	// 本周第一天
	public static String WeekFirstDay() {
		Calendar cal = Calendar.getInstance();
		int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;
		cal.add(Calendar.DATE, -day_of_week);

		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.UK);

		return df.format(cal.getTime());
	}

	// 本周最后一天
	public static String WeekLastDay() {
		Calendar cal = Calendar.getInstance();
		int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2;

		cal.add(Calendar.DATE, -day_of_week);
		cal.add(Calendar.DATE, 6);

		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.UK);

		return df.format(cal.getTime());
	}

	// 本月第一天
	public static String MonthFirstDay() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, 0);
		cal.set(Calendar.DAY_OF_MONTH, 1);

		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.UK);

		return df.format(cal.getTime());
	}

	// 本月最后一天
	public static String MonthLastDay() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, 1);
		cal.set(Calendar.DAY_OF_MONTH, 0);

		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.UK);

		return df.format(cal.getTime());
	}

	public static Date getAfterMonth(int month) {
		Date d = new Date();
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.MONTH, now.get(Calendar.MONTH) + month);

		return now.getTime();
	}

	public static Date getAfterDay(int day) {
		Date d = new Date();
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH) + day);

		return now.getTime();
	}

	public static Date getAfterMinute(int minute) {
		Date d = new Date();
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + minute);

		return now.getTime();
	}

	public static Date getAfterHours(int hours) {
		Date d = new Date();
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.HOUR, now.get(Calendar.HOUR) + hours);

		return now.getTime();
	}

	public static Date getAfterSecond(int Second) {
		Date d = new Date();
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.SECOND, now.get(Calendar.SECOND) + Second);

		return now.getTime();
	}

	public static int getHours(Date start, Date end) {
		long time = end.getTime() - start.getTime();
		long hours = time / (1000 * 60 * 60);
		return (int) hours;
	}

	@SuppressWarnings("deprecation")
	public static int isPushDate() {
		Integer nowTime = new Date().getHours();

		if (nowTime < 23 && nowTime > 8) {
			return 1;
		}
		return 0;
	}

	public static int compareDate(Date date, Date now) {
		String nowString = dateFormat(now, datePattern);
		String dateString = dateFormat(date, datePattern);

		Date nowDate = null, myDate = null;
		SimpleDateFormat myFormatter = new SimpleDateFormat(datePattern);

		try {
			nowDate = myFormatter.parse(nowString);
			myDate = myFormatter.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		int day = (int) ((myDate.getTime() - nowDate.getTime()) / (24 * 60 * 60 * 1000));
		return day;
	}

	public static int compare(Date date1, Date date2) {
		String date1String = dateFormat(date1, datePattern);
		String date2String = dateFormat(date2, datePattern);

		SimpleDateFormat myFormatter = new SimpleDateFormat(datePattern);

		try {
			date1 = myFormatter.parse(date1String);
			date2 = myFormatter.parse(date2String);
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		int day = (int) ((date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000));
		return day;
	}

	public static Date getDate(String date, String datePattern) {
		if (datePattern == null)
			datePattern = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.UK);
		Object value = date;
		if (value == null) {
			return null;
		} else if (value instanceof Date) {
			return (Date) value;
		} else if (value instanceof String) {
			try {
				return df.parse(date);
			} catch (ParseException e) {
				throw new RuntimeException(e);
			}
		}

		return (Date) value;
	}


	public static Date getDate(String datePattern) {
		if (datePattern == null) {
			datePattern = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.UK);
		Object value = dateFormatNow( datePattern );
		if (value == null) {
			return null;
		} else if (value instanceof Date) {
			return (Date) value;
		} else if (value instanceof String) {
			try {
				return df.parse(dateFormatNow( datePattern ));
			} catch (ParseException e) {
				throw new RuntimeException(e);
			}
		}

		return (Date) value;
	}

	public static Date getDate(Date date,String datePattern) {
		if (datePattern == null) {
			datePattern = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat df = new SimpleDateFormat(datePattern, Locale.UK);

		Object value = dateFormat( date );
		if (value == null) {
			return null;
		} else if (value instanceof Date) {
			return (Date) value;
		} else if (value instanceof String) {
			try {
				return df.parse(dateFormatNow( datePattern ));
			} catch (ParseException e) {
				throw new RuntimeException(e);
			}
		}

		return (Date) value;
	}

}
